package com.zzz.demo.configuration;

import com.taobao.txc.client.aop.TxcTransactionScaner;
import com.taobao.txc.datasource.cobar.TxcDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @desc 阿里分布式事物管理配置
 * @author ZZZ
 * @date 2018/9/26 026
 * @version 1.0
 */
//@Configuration
public class DistributTransactionConfig {

    private static final String APP_NAME = "demo";
    private static final String TX_GROUP_NAME = "txc_test_public.1129361738553704.QD";
    private static final String ACC_KEY = "zzz";
    private static final String SEC_KEY = "admin";

    @Value("${spring.datasource.url:#{null}}")
    private String dbUrl;
    @Value("${spring.datasource.username: #{null}}")
    private String username;
    @Value("${spring.datasource.password:#{null}}")
    private String password;
    @Value("${spring.datasource.driverClassName:#{null}}")
    private String driverClassName;

    @Bean
    public TxcDataSource getTxDataSource() {
        TxcDataSource txcDataSource = new TxcDataSource();
        txcDataSource.setUrl(this.dbUrl);
        txcDataSource.setUsername(username);
        txcDataSource.setPassword(password);
        txcDataSource.setDriverClassName(driverClassName);
        return txcDataSource;
    }

    @Bean
    public TxcTransactionScaner getTxTransactionScaner() {
        TxcTransactionScaner txcTransactionScaner = new TxcTransactionScaner(APP_NAME, TX_GROUP_NAME, 1);
        txcTransactionScaner.setAccessKey(ACC_KEY);
        txcTransactionScaner.setSecretKey(SEC_KEY);
        return txcTransactionScaner;
    }
}
